﻿using System;
using PhiStore.Util;

namespace PhiStore.Security
{
    /// <summary>
    /// This is the abstract class which implements the Credentials used for the PhiStore
    /// </summary>
    [Serializable]
    public abstract class PhiCredentials : IEquatable<PhiCredentials>
    {
        private string username;
        private byte[] password;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        protected PhiCredentials(string username, string password)
        {
            this.username = username;
            this.password = Helper.MD5(password);
        }

        /// <summary>
        /// Username
        /// </summary>
        public string Username { get { return username; } }

        /// <summary>
        /// Password
        /// </summary>
        public byte[] Password { get { return password; } }

        /// <summary>
        /// Is it client credentials?
        /// </summary>
        abstract public bool IsClientCredentials { get; }

        /// <summary>
        /// Is it server credentials?
        /// </summary>
        public bool IsServerCredential { get { return !IsClientCredentials; } }

        /// <summary>
        /// is it an anonymous credential object
        /// </summary>
        public bool Anonymous { get { return username == ""; } }

        /// <summary>
        /// Compares two PhiCredentials
        /// </summary>
        /// <param name="other">the PhiCredentials to compare to</param>
        /// <returns>whether or not they are equal</returns>
        public bool Equals(PhiCredentials other)
        {
            return this.Username == other.Username &&
                Compare.ArrayEquals(this.Password, other.Password) &&
                this.IsClientCredentials == other.IsClientCredentials;
        }

    }
}
